package com.teradata

import java.io.{File, PrintWriter}

import scala.collection.mutable
import scala.io.Source

object IopTmpOpt {
  def main(args: Array[String]): Unit = {
    val fileName = "/data/iop/IOP_TD/bin/select_phone.txt"
    val source = Source.fromFile(fileName)
    val lines = source.getLines

    val saleMap: mutable.HashMap[String, List[(String, String)]] = new scala.collection.mutable.HashMap
    // 0013404800100                                   column=0:3ACC, timestamp=123456789, value=201804
    var count = 0
    lines.foreach(line => {
      if (line.matches("(?im)^\\s\\d+\\s+column.*")) {
        val items = line.split("\\s+")
        val phoneNo = items(1)
        val family = items(2).replaceAll("(?im)column=|,", "")
        val value = items(4).replaceAll("value=", "")
        if (saleMap.contains(phoneNo)) {
          val lastList: List[(String, String)] = saleMap(phoneNo)
          saleMap.update(phoneNo, (family, value) +: lastList)
        } else {
          saleMap.update(phoneNo, List((family, value)))
        }
        count = count + 1
        if (count % 100000 == 0) println(count)
      }
    })

    val writer = new PrintWriter(new File("/data/iop/IOP_TD/bin/selectPhoneResult.txt"))
    saleMap
      .filter((f: (String, List[(String, String)])) => {
        if (f._2.map(_._2).contains("201806")) false else true
      }).foreach(line => {
      val phoneNo = line._1
      val list = line._2
      list.foreach(kv => {
        val family = kv._1
        val value = kv._2
        writer.println(s"put 'SelectPhoneList','$phoneNo','$family','$value',123456789")
      })
    })
    writer.close()

    source.close

  }
}
